package com.xiaoyu.lib_av.manager

import `in`.srain.cube.util.CLog
import com.xiaoyu.lib_av.proxy.AgoraAVRoomProxy
import `in`.srain.cube.concurrent.AppThreads
import com.xiaoyu.lib_av.datamodel.AVRoomPosition
import com.xiaoyu.lib_av.datamodel.message.AVRoomMessage
import com.xiaoyu.lib_av.listener.IAVRoomLifecycleListener

class AVRoomManager private constructor() : IAVRoomLifecycleListener {

    private var mProxy: AgoraAVRoomProxy? = null
    private var mAVRoomLifecycleListener: IAVRoomLifecycleListener? = null

    fun init() {
        mProxy = AgoraAVRoomProxy()
    }

    fun create(lifecycleListener: IAVRoomLifecycleListener) {
        init()
        mProxy?.onCreate()
        mAVRoomLifecycleListener = lifecycleListener
    }

    fun destroy() {
        mProxy?.onDestroy()
        mAVRoomLifecycleListener = null
    }

    fun joinRoom(channelId: String?, runnable: Runnable?) {
        channelId ?: return
        mProxy?.joinRoom(channelId, runnable)
    }

    fun leaveRoom(channelId: String?) {
        mProxy?.leaveRoom(channelId)
    }

    fun joinChannel(token: String?, channelId: String?, uid: String) {
        mProxy?.joinChannel(token, channelId, uid)
    }

    fun setPosition(@AVRoomPosition.AVRoomPositionDef position: String) {
        mProxy?.setPosition(position)
    }

    fun setSpeakerEnable(enable: Boolean) {
        mProxy?.isEnableSpeaker = enable
    }

    fun setMute(mute: Boolean) {
        mProxy?.setMute(mute)
    }

    fun isCalling(): Boolean {
        return AgoraManager.getInstance().isCalling
    }

    override fun onAudioDeviceChanged(headset: Boolean) {
        AppThreads.runOnMainThread {
            mAVRoomLifecycleListener?.onAudioDeviceChanged(headset)
        }
    }

    override fun onNetStatusReported(good: Boolean, uid: String) {

    }

    override fun onPositionChange(isAudience: Boolean) {
        AppThreads.runOnMainThread {
            mAVRoomLifecycleListener?.onPositionChange(isAudience)
        }
    }

    override fun onAnnouncerJoin(fuid: String) {
        AppThreads.runOnMainThread {
            mAVRoomLifecycleListener?.onAnnouncerJoin(fuid)
        }
    }

    override fun onJoinChannelSuccess(uid: Int) {
        AppThreads.runOnMainThread {
            mAVRoomLifecycleListener?.onJoinChannelSuccess(uid)
        }
    }

    override fun onConnectionStateChanged(state: Int, reason: Int) {
        AppThreads.runOnMainThread {
            mAVRoomLifecycleListener?.onConnectionStateChanged(state, reason)
        }
    }

    override fun onAnnouncerLeave(targetUid: String) {
        AppThreads.runOnMainThread {
            mAVRoomLifecycleListener?.onAnnouncerLeave(targetUid)
        }
    }

    override fun updateMemberCount(memberCount: Int) {
        AppThreads.runOnMainThread {
            mAVRoomLifecycleListener?.updateMemberCount(memberCount)
        }
    }

    override fun onMessageSend(message: AVRoomMessage) {
        AppThreads.runOnMainThread {
            mAVRoomLifecycleListener?.onMessageSend(message)
        }
    }

    override fun onMessageReceive(message: AVRoomMessage) {
        AppThreads.runOnMainThread {
            mAVRoomLifecycleListener?.onMessageReceive(message)
        }
    }

    fun sendMessage(message: AVRoomMessage) {
        CLog.i(TAG, "sendMessage() message=%s", message)
        mProxy?.sendMessage(message)
    }

    override fun onAudioVolumeChange(audioVolumeIndicatorList: MutableList<AudioVolumeIndicator>?, isSelf: Boolean) {
        mAVRoomLifecycleListener?.onAudioVolumeChange(audioVolumeIndicatorList, isSelf)
    }

    companion object {

        private val TAG = "AVRoomManager"
        private val INSTANCE = AVRoomManager()

        fun getInstance(): AVRoomManager {
            return INSTANCE
        }

        class AudioVolumeIndicator(var uid: String, val isSpeaking: Boolean)
    }
}
